package logic.SysRole

import cn.ibizlab.util.domain.EntityBase
import cn.ibizlab.util.helper.RuleUtils
import cn.ibizlab.util.security.AuthenticationUser
import cn.ibizlab.util.security.SpringContextHolder
import cn.ibizlab.core.authorization.domain.SysRole
import cn.ibizlab.core.authorization.service.SysRoleService
import cn.ibizlab.core.authorization.domain.SysRole
import cn.ibizlab.core.authorization.service.SysRoleService
import com.alibaba.fastjson.JSONObject
import org.springframework.util.ObjectUtils

/**
 *  删除前的检查
 */
class CheckBeforeRemove {

    //当前用户
    def curuser = AuthenticationUser.getAuthenticationUser()
    //传入变量
    def _default
    def _roleadmin = new EntityBase()
    def _roleusers = new EntityBase()
    //sysrole
    def _check = new SysRole()
    def _flag = new EntityBase()
    //返回参数
    def _return

    def execute(arg) {
        println 'running....'
        _default = arg
        
        _return = arg
        begin()
        println 'running end....'
        return _return
    }

    //开始
    def begin() {
    
        //连接名称
        if(true) {
            deaction1()
        }
    }

    //抛出异常
    def throwexception1() {
        throw new RuntimeException('工作流角色不允许删除!')
        if(true) {
            end1()
        }
    }

    //抛出异常
    def throwexception2() {
        throw new RuntimeException('预定义角色不允许删除')
        if(true) {
            end1()
        }
    }

    //实体行为
    def deaction1() {
        SpringContextHolder.getBean(SysRoleService.class).get(_default)

        //连接名称
        if((((RuleUtils.test(_check, 'ISNOTNULL', 'null')) && (RuleUtils.test(_check.get('workflow_group'), 'ISNOTNULL', 'null')) && (RuleUtils.test(_check.get('workflow_group'), 'EQ', '1'))))) {
            throwexception1()
        }
        //连接名称
        else if((((RuleUtils.test(_check, 'ISNULL', 'null')) || (RuleUtils.test(_check.get('workflow_group'), 'ISNULL', 'null')) || (RuleUtils.test(_check.get('workflow_group'), 'NOTEQ', '1'))))) {
            rawsfcode1()
        }
    }

    //执行脚本代码
    def rawsfcode1() {
        if(_check!=null && _check.getSystemId()!=null && _check.getAuthority()!=null
    && (_check.getAuthority().equals(String.format("ROLE_%1$s_ADMIN",_check.getSystemId().toUpperCase()))
    ||_check.getAuthority().equals(String.format("ROLE_%1$s_USERS",_check.getSystemId().toUpperCase()))
    ||_check.getAuthority().equals("ROLE_USERS")
    ||_check.getAuthority().equals("ROLE_SUPERADMIN")
    ||_check.getAuthority().equals("ROLE_APIUSERS"))){
    flag = "true";
    }
drools.getKieRuntime().setGlobal("_flag",_flag);


        //连接名称
        if(((RuleUtils.test(_flag, 'NOTEQ', 'true')))) {
            end1()
        }
        //连接名称
        else if(((RuleUtils.test(_flag, 'EQ', 'true')))) {
            throwexception2()
        }
    }

    //结束
    def end1() {

    }

}
